<?php
/**
 * Description
 *
 * PHP version 7
 *
 * @category Custom
 * @package  Custom
 * @author   ryu <mo5467@126.com>
 * @date     2020-04-25 12:15:07
 * @lastTime 2020-04-26 01:43:49
 * @license  https://opensource.org/licenses/MIT MIT License
 * @link     none
 */

namespace Design\Structural\Flyweight;

class TextFactory implements \Countable
{

    private $textPool = [];


    public function get(string $name): Text
    {
        if (isset($this->textPool[$name]) === false) {
            $this->textPool[$name] = $this->create($name);
        }

        return $this->textPool[$name];

    }//end get()


    private function create(string $name): Text
    {
        if (strlen($name) === 1) {
            return new Character($name);
        } else {
            return new Word($name);
        }

    }//end create()


    public function count(): int
    {
        return count($this->textPool);

    }//end count()


}//end class
